﻿using Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Utility;
using System.Utility.DBMS;
using System.Utility.Emailing;
using System.Utility.Messaging;
using System.Utility.UI;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUI.Classes;

namespace WebUI
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string pageurl = Constant.LoginUrl;
            try
            {

                AppUser u = AppUser.GetUser(username.Value, password.Value);
                
                //AppUser u = AppUser.GetUser("visha.kumar1145@gmail.com", "test");

                if (u == null)
                    PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
                else
                {
                    StateManager<AppUser>.SetSessionState(SessionKey.UserInfo, u);
                    switch (u.Type)
                    {
                        case AppUserType.AppUser:
                            pageurl = Constant.Link.LandingLink;
                            break;
                    }
                    Response.Redirect(pageurl);
                }
            }
            catch (Exception ex)
            {
                MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
            }

        }

        string rndPassword = "random@123";
        protected void btnRegister_Click(object sender, EventArgs e)
        {

            //string pageurl = Constant.LoginUrl;
            //try
            //{
            //    AppUser u = AppUser.GetUser(username.Value, password.Value);

            //    if (u == null)
            //        PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
            //    else
            //    {
            //        StateManager<AppUser>.SetSessionState(SessionKey.UserInfo, u);
            //        switch (u.Type)
            //        {
            //            case AppUserType.AppUser:
            //                pageurl = "default.aspx";
            //                break;
            //        }
            //        Response.Redirect(pageurl);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageUtility.LogAndGetMessageCode(ex);
            //    PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
            //}


            try
            {
                // need to change the password mechanism
                DBHelper.ExcuteProc("AddUser", new object[] { user_email.Value, rndPassword, fullname.Value });


                try
                {
                    StandardTemplateContentLoader loader = new StandardTemplateContentLoader("ResetEmail");
                    EmailDataContainer container = new EmailDataContainer(loader);
                    StandardTemplateEmailer emailer = new StandardTemplateEmailer(container);
                    emailer.MailContentReplace += new MailContentReplaceEventHandler(reader_MailContentReplace);
                    emailer.ToMail += new MailEventHandler(chckerToMail);
                    emailer.Send();
                    Logger.LogMessage("email sent to - " + username.Value, LogLevel.DEBUG, LogMessageType.ApplicationMessage);
                }
                catch (Exception exInner)
                {

                }
                string pageurl = Constant.LoginUrl;
                AppUser u = AppUser.GetUser(user_email.Value, rndPassword);
                StateManager<AppUser>.SetSessionState(SessionKey.UserInfo, u);
                switch (u.Type)
                {
                    case AppUserType.AppUser:
                        pageurl = Constant.Link.LandingLink;
                        break;
                }
                Response.Redirect(pageurl);

            }
            catch (Exception ex)
            {
                MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RefreshByType(new Control[] { user_email, fullname });
                PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
            }
            //PageUtility.RefreshByType(new Control[] { user_email, fullname });
            //PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");


        }

        void chckerToMail(object sender, MailEventArgs eventArgs)
        {
            eventArgs.MyMail.To.Clear();
            eventArgs.MyMail.To.Add(user_email.Value);
        }

        void makerCheckerReplace(object sender, MailContentEventArgs e)
        {
            //e.Content = e.Content.Replace("$$vishal$$", "test");
        }

        void reader_MailContentReplace(object sender, MailContentEventArgs e)
        {
            //ActivationLink link = new ActivationLink(username.Value);
            //string activatePath = link.GenerateLink();

            e.Content = e.Content.Replace("$$ResetPasswordLink$$", rndPassword);
        }
        
    }
}